package FilePackage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import ObjectPackage.ObjectClass;

public class FileClass {

	private String FileName;
	private final String DefaultName="List.txt";
	private ArrayList<ObjectClass> arr = new ArrayList<ObjectClass>() ;
	public ArrayList<ObjectClass> getArr()
	{
		return arr;
	}
	public void setArr(ArrayList<ObjectClass> ob)
	{
		arr = ob;
	}
	public void clear()
	{
		arr.clear();
	}
	public void setFileName(String arg)
	{
		this.FileName = arg;
	}
	public String getFileName()
	{
		return FileName;
	}
	public String gFileName()
	{
		if  (FileName ==null|| FileName.isEmpty())
		{
            return DefaultName;
		}else{
            File file =new File(FileName);
            if(file.isFile()){
                    try{
                            read(FileName);
                            return FileName;
                    }
                    catch(Exception e)
                    {
                            System.out.println("Exception with file!");
                            return "";
                    }
            }
            else if(file.isDirectory()){
                 try{
                                    FileName +="\\"+DefaultName;
                                    file =new File(FileName);
                                    file.createNewFile();   
                                    return FileName;
                            }catch(IOException e){
                                    System.out.println("Not found file!");
                                    return "";
                            }
                    }
                    else{
                            System.out.println("Not found directory! ");
                            return "";
                    }
            }
    }                               


	/**
	 * Reading of file
     *@Parameter String
     *@Parameter ArrayList<ObjectClass>
     */
	public ArrayList<ObjectClass> read(String fileName) 
	{
	     
         String detail =new String();
         ObjectClass ob = null;
         BufferedReader buf =null;
         try{
        	 buf =new BufferedReader(new FileReader(fileName));
        	  while((detail = buf.readLine())!=null){
                     if(!detail.equals("")){
                             ob = new ObjectClass();
                             ob.fillObject(detail);
                             arr.add(ob);
                     }
             }
             
         }
         catch(Exception e){
             if(buf !=null)
                     try{
                    	 buf.close();
                     }catch(IOException e1){
                             e1.printStackTrace();
                     }
             System.out.println(e.getMessage());
         }
         setArr(arr);
         return arr;
	}

	/**
	 * Adding of new records
     *@Parameter String
     *@Parameter ObjectClass 
     */
	public void AddNew(ObjectClass object,String filename){      
	         try{
	        	 FileWriter out =new FileWriter(new File(filename),true);
	        	 try{
	        		
	        			 out.write(object.ToString());
	           		     out.write(System.getProperty("line.separator"));
	           		
	                 
	        	 }finally{
	                 out.close();
	        	 }
	         }catch(IOException e){
	                 throw new RuntimeException(e);
	         }
	 }

	

}

